package action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import service.SaleService;
import vo.Saleorder;
import vo.Saleorderitem;
import vo.Staff;
import vo.Payment;

public class SaleAction {
	Saleorder saleOrder;
	SaleService service;
	Staff staff;
	Payment payment;
	private JSONArray root;
	List inputdata;
	int inputSize;
	List list = new ArrayList();
	
	public String createSale(){
		list = new ArrayList();
		Map<String,Object> map = new HashMap<String,Object>();
		map = service.createOrder(saleOrder, staff);
		list.add(map.get("saleNo"));
		Saleorder order = (Saleorder) map.get("saleOrder");
		JSONArray input = JSONArray.parseArray(JSON.toJSONString(inputdata,false));
		JSONObject jso=JSON.parseObject(input.getString(0));
		Saleorderitem item = new Saleorderitem();
		for(int i = 0 ; i < inputSize ; i++){
			item.setSaleorder(order);
			item.setItemId(jso.getJSONObject(""+i+"").get("itemId").toString());
			item.setQuantity(Integer.parseInt(jso.getJSONObject(""+i+"").get("quantity").toString()));
			item.setSalePrice(Double.parseDouble(jso.getJSONObject(""+i+"").get("price").toString()));
			item.setSpec(jso.getJSONObject(""+i+"").get("spec").toString());
			item.setSweetness(jso.getJSONObject(""+i+"").get("sweetness").toString());
			item.setTemperature(jso.getJSONObject(""+i+"").get("temperature").toString());
			list.add(service.addItem(item));
		}
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String getAllOrder(){
		list = service.getAllOrder();
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String getItem(){
		list = service.getItem(saleOrder.getSaleOrderNo());
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public String addPayment(){
		list = service.addPayment(payment,staff,saleOrder);
		root = JSONArray.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect));
		return "success";
	}
	
	public Saleorder getSaleorder() {
		return saleOrder;
	}
	public void setSaleorder(Saleorder saleOrder) {
		this.saleOrder = saleOrder;
	}
	public SaleService getService() {
		return service;
	}
	public void setService(SaleService service) {
		this.service = service;
	}
	public Staff getStaff() {
		return staff;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	public JSONArray getRoot() {
		return root;
	}
	public void setRoot(JSONArray root) {
		this.root = root;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}


	public Saleorder getSaleOrder() {
		return saleOrder;
	}


	public void setSaleOrder(Saleorder saleOrder) {
		this.saleOrder = saleOrder;
	}


	public List getInputdata() {
		return inputdata;
	}


	public void setInputdata(List inputdata) {
		this.inputdata = inputdata;
	}


	public int getInputSize() {
		return inputSize;
	}


	public void setInputSize(int inputSize) {
		this.inputSize = inputSize;
	}

	public Payment getPayment() {
		return payment;
	}

	public void setPayment(Payment payment) {
		this.payment = payment;
	}
	
}
